package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.role.RoleDTO;
import com.qk.entity.Role;
import com.qk.management.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/roles")
@SuppressWarnings("all")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @GetMapping
    public Result listRoles(RoleDTO roleDto){
        PageResult<Role> pageResult = roleService.listRoles(roleDto);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result addRole(@RequestBody Role role){
        roleService.addRole(role);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteRole(@PathVariable(name = "id") Integer id){
        roleService.deleteRole(id);
        return Result.success();
    }

    @PutMapping
    public Result updateRole(@RequestBody Role role){
        roleService.updateRole(role);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable(name = "id") Integer id){
        Role role = roleService.getById(id);
        return Result.success(role);
    }

    @GetMapping("/list")
    public Result getAllRoles(){
        List< Role> roles = roleService.getAllRoles();
        return Result.success(roles);
    }
}
